//身份证校验
function checkSfzCode(code) { 
	var tip = "";
	if(code!=""){
		
	
            var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
            
            var pass= true;
            
		    //是否为空  
		    if(code === '')  
		    {  
		        tip = "请输入身份证号，身份证号不能为空";
                pass =false;
		    }  
		    //校验长度，类型  
		    else if(isCardNo(code) === false)  
		    {  
		        tip = "您输入的身份证号码不正确，请重新输入";
                pass =false; 
		    }  
		    //检查省份  
		    else if(checkProvince(code,city) === false)  
		    {  
		        tip = "您输入的身份证号码不正确,请重新输入";
                pass =false; 
		    }  
		    //校验生日  
		    else if(checkBirthday(code) === false)  
		    {  
		        tip = "您输入的身份证号码生日不正确,请重新输入";
                pass =false; 
		    }  
            else{
                //18位身份证需要验证最后一位校验位
                if(code.length == 18){
                    code = code.split('');
                    //∑(ai×Wi)(mod 11)
                    //加权因子
                    var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                    //校验位
                    var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                    var sum = 0;
                    var ai = 0;
                    var wi = 0;
                    for (var i = 0; i < 17; i++)
                    {
                        ai = code[i];
                        wi = factor[i];
                        sum += ai * wi;
                    }
                    var last = parity[sum % 11];
                    if(parity[sum % 11] != code[17]){
                        tip = "身份证格式错误";
                        pass =false;
                    }
                }
            }
            return pass;
            }
        }
//检查号码是否符合规范，包括长度，类型  
isCardNo = function(card)  
{  
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;  
    if(reg.test(card) === false)  
    {  
        return false;  
    }  
  
    return true;  
};  
//取身份证前两位,校验省份  
checkProvince = function(card,city)  
{  
    var province = card.substr(0,2);  
    if(city[province] == undefined)  
    {  
        return false;  
    }  
    return true;  
};  
//检查生日是否正确  
checkBirthday = function(card)  
{  
    var len = card.length;  
    //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字  
    if(len == '15')  
    {  
        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;   
        var arr_data = card.match(re_fifteen);  
        var year = arr_data[2];  
        var month = arr_data[3];  
        var day = arr_data[4];  
        var birthday = new Date('19'+year+'/'+month+'/'+day);  
        return verifyBirthday('19'+year,month,day,birthday);  
    }  
    //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X  
    if(len == '18')  
    {  
        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;  
        var arr_data = card.match(re_eighteen);  
        var year = arr_data[2];  
        var month = arr_data[3];  
        var day = arr_data[4];  
        var birthday = new Date(year+'/'+month+'/'+day);  
        return verifyBirthday(year,month,day,birthday);  
    }  
    return false;  
};  
//校验日期  
verifyBirthday = function(year,month,day,birthday)  
{  
    var now = new Date();  
    var now_year = now.getFullYear();  
    //年月日是否合理  
    if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)  
    {  
        //判断年份的范围（3岁到100岁之间)  
        var time = now_year - year;  
        if(time >= 3 && time <= 100)  
        {  
            return true;  
        }  
        return false;  
    }  
    return false;  
};  
//字母、数字、下划线组成，字母开头，4-16位。
function checkUserName(str){
    var re = /^[a-zA-z]\w{3,15}$/;
    var tip = "";
    if(re.test(str)){
    	tip = "";
    }else{
        tip = "组成非法，构成：字母、数字、下划线组成，字母开头，4-16位。";
    }          
    return tip;
}
//手机号码 2016年10月8日 添加正则验证 17 开发人: 秦臻 
function  checkTelPhone(str) {
	return /^(13|15|17|18)\d{9}$/i.test(str);
}
//固定电话校验
function checkPhone(str){
    var re = /^0\d{2,3}-?\d{7,8}$/;
    var tip = "";
    if(re.test(str)){
    	tip = "";
    }else{
        tip = "固定电话错误";
    }          
    return tip;
}
//邮箱验证
function checkEmail(str){
    var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
    var tip = "";
    if(re.test(str)){
    	tip = "";
    }else{
        tip = "邮箱格式错误";
    }          
    return tip;
}
//邮编
function checkYzbm(str){
    var re= /^[1-9][0-9]{5}$/;
    var tip="";
    if(re.test(str)){
    	tip = "";
    }else{
        tip = "邮编格式错误";
    }          
    return tip;
}

//校验数字
function checkSz(str){
	var tip = "";
	if(isNaN(str)){
		tip = "请输入数字";
	}
	return tip;
}
//校验正整数
function checkZzs(str){
	var re= /^[0-9]*$/;
    var tip="";
    if(!re.test(str)){
		tip = "请输正整数";
	}
	return tip;
}
function validateNum(val){//验证小数，保留二位小数点  
	var patten = /^-?\d+\.?\d{0,4}$/;  
	return patten.test(val);  
}  
//是否汉字
function CheckChinese(val){  
	var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
	if(reg.test(val)){
      return true;
	}       
	return false;
}

function checkPhone(v){  
	var reg=/((15)\d{9})|((13)\d{9})|((18)\d{9})|((17)\d{9})|(0[1-9]{2,3}\-?[1-9]{6,7})/i;  
	if(reg.test(v)){  
		return true;  
	}else{  
		return false;  
	}  
}  
//验证营业执照
function isValidBusCode(busCode){
    var ret=false;
    if(busCode.length==15){
        var sum=0;
        var s=[];
        var p=[];
        var a=[];
        var m=10;
        p[0]=m;
        for(var i=0;i<busCode.length;i++){
           a[i]=parseInt(busCode.substring(i,i+1),m);
           s[i]=(p[i]%(m+1))+a[i];
           if(0==s[i]%m){
             p[i+1]=10*2;
           }else{
             p[i+1]=(s[i]%m)*2;
            }    
        }                                       
        if(1==(s[14]%m)){
           //营业执照编号正确!
          //  alert("营业执照编号正确!");
        }else{
           //营业执照编号错误!
            ret=true;
           // alert("营业执照编号错误!");
         }
    }//如果营业执照为空
   else if(""==busCode){
        ret=true;
    }else{
        ret=true;
       // alert("营业执照格式不对，必须是15位数的！");
    }
    return ret;
}
//验证统一社会信用代码
function checkBusinessCode(orgCode) {
	var ret = false;
    if (orgCode.length != 18) {
       // return "统一社会信用代码长度必须为18位.";
    	ret = true;
    }
    //字符集数组
    var fin_array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'];
    //加权数组
    var i_array = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
    //结果数组
    var val_array = [];
    //相乘数组
    var ch_array = [];
    //Sigma
    var sigma = 0;
    //部分IE浏览器不支持数组中的indexOf函数, 故加上此代码;
    if (!Array.indexOf) {
        Array.prototype.indexOf = function (obj) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] == obj) {
                    return i;
                }
            }
            return -1;
        }
    }
    //转换成字符集的值
    for (var i = 0; i < 17; i++) {
        val_array[i] = fin_array.indexOf(orgCode.charAt(i));
    }
    //校验是否包含字符集之外的字符
    for (var i = 0; i < 17; i++) {
        if (val_array[i] == -1) {
            //return "包含非法字符";
        	ret = true;
        }
    }
    //生成加权结果
    for (var i = 0; i < 17; i++) {
        ch_array[i] = val_array[i] * i_array[i];
    }
    //求出Sigma
    for (var i = 0; i < 17; i++) {
        sigma = sigma + ch_array[i];
    }
    //求出校验位
    //alert(sigma);
   // alert(1495 % 31);
    var yushu = sigma % 31==0?31:sigma % 31;
    var vali = fin_array[31 - yushu];
    //查看校验码是否正确
    //alert(orgCode.charAt(17)+"::"+vali);
    if (orgCode.charAt(17) == vali) {
        //return "正确";
    } else {
       // return "校验码错误: 应该为: " + vali;
        ret = true;
    }
    return ret;
}


